package com.mavenq.fly.websocket.handler;

import com.mavenq.fly.utils.JsonUtils;
import com.mavenq.fly.websocket.service.WSConnectionService;
import com.mavenq.fly.websocket.entity.WSMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

@Slf4j
@Component
public class EchoWebSocketHandler extends AbstractWSHandler {

    public EchoWebSocketHandler(WSConnectionService connectionService) {
        super(connectionService);
    }

    @Override
    protected void onMessage(WebSocketSession session, WSMessage msg) {
        if ("ping".equalsIgnoreCase(msg.getType())) {
            sendPong(session);
            return;
        }
        // 原样回显
        sendText(session, JsonUtils.toJson(msg));
    }
}